<template>
  <div class="my-tag">
    <input
      @blur="isEdit = false"
      @change="handleChange"
      ref="inp"
      :value="value"
      v-if="isEdit"
      class="input"
      type="text"
      placeholder="输入标签"
    />
    <div v-else class="text" @dblclick="handleDoubleClick">{{value}}</div>
  </div>
</template>

<script>
export default {
    props:{
        value:String,
    },
  data() {
    return {
      isEdit: false,
    };
  },
  methods: {
    handleChange(e) {
      //通知父组件修改
      this.$emit('input',e.target.value);
      this.isEdit = false;
    },
    handleDoubleClick() {
      this.isEdit = true;
      //等dom更新之后将焦点放置到input元素上
      this.$nextTick(() => {
        this.$refs.inp.focus();
      });
    },
  },
};
</script>

<style>
</style>
